package com.wangyadong.hobby.schedule.jobtask.dsa.lesson1.exampl;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/1/1413:40
 **/
public class CaesarCipher {


    private static final Character[] encoder = new Character[26];
    private static final Character[] decoder = new Character[26];

    public CaesarCipher(int round) {
        for (int i = 0; i < 26; i++) {
            encoder[i] = (char) ('A'+(i + round) % round);
            decoder[i] = (char) ('A'+(i - round + 26) % round);
        }
    }

    public String encode(String msg) {
        return transform(msg, encoder);
    }


    public String deode(String msg) {
        return transform(msg, decoder);
    }

    public String transform(String orignal, Character[] characters) {
        char[] msg = orignal.toCharArray();
        for (int i = 0; i < msg.length; i++) {
            if (Character.isUpperCase(msg[i])) {
                int j = msg[i] - 'A';
                msg[i] = characters[j];
            }
        }
        return new String(msg);
    }

    public static void main(String[] args) {
        CaesarCipher caesarCipher=new CaesarCipher(20);
        String abcdefg = caesarCipher.encode("WANGYADONG");
        System.out.println(abcdefg);
        String deode = caesarCipher.deode(abcdefg);
        System.out.println(deode);
    }

}
